package com.oitsm.dispatcher.task;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.oitsm.collection.CallbackAble;
import com.oitsm.plugin.entity.Context;
import com.oitsm.plugin.entity.PluginMeta;
import com.oitsm.plugin.entity.Response;
import com.oitsm.plugin.interfaces.Plugin;

/**
 * 采集任务
 * @author Arab Runner
 * Jul 24, 2011 2:45:41 PM
 */
public class CollectTasker extends Tasker{
	public static Logger logger = Logger.getLogger(CollectTasker.class.getName());
	
	private PluginMeta meta = null;
	private Plugin plugin = null;
	private Context ctx = null;
	private Response res = null;
	
	private List<CallbackAble> callbacks = new ArrayList<CallbackAble>();
	
	public CollectTasker(PluginMeta meta,Plugin p,Context ctx){
		this.meta = meta;
		this.plugin = p;
		this.ctx = ctx;
		res = new Response();
	}
	
	public void addCallback(CallbackAble c){
		this.callbacks.add(c);
	}

	public void run() {
		try {
			plugin.excute(meta,ctx,res);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(callbacks.size()>0){
				for(CallbackAble c : callbacks){
					try{
						c.call(ctx, res,this);
					}catch(Throwable t){
						logger.error(t.getMessage());
					}
				}
			}
		}
	}

	public PluginMeta getMeta() {
		return meta;
	}

	public Plugin getPlugin() {
		return plugin;
	}
}
